package com.qn.wechat.service;

import com.jeesuite.confcenter.ConfigChangeHanlder;
import com.jeesuite.mybatis.plugin.pagination.Page;
import com.jeesuite.spring.ApplicationStartedListener;
import com.jeesuite.springweb.model.WrapperResponse;
import com.oneplatform.base.exception.AssertUtil;
import com.qn.wechat.dao.entity.WeChatAccountEntity;
import com.qn.wechat.dao.mapper.WeChatAccountMapper;
import com.qn.wechat.ifservice.dto.WechatAccountDto;
import com.qn.wechat.ifservice.dto.para.WeChatAccountQueryPara;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
public class WeChatAccountService implements ConfigChangeHanlder, ApplicationStartedListener {

    @Resource
    WeChatAccountMapper weChatAccountMapper;

    @Resource
    IdService idService;

    @Override
    public void onConfigChanged(Map<String, Object> map) {

    }

    @Override
    public void onApplicationStarted(ApplicationContext applicationContext) {

    }


    public Page<WechatAccountDto> getWeChatAccountList(WeChatAccountQueryPara para) {
        Page<WeChatAccountEntity> list = weChatAccountMapper.findWeChatAccountList(para, para);
        Page<WechatAccountDto> res = new Page<>();
        if (list != null)
            BeanUtils.copyProperties(list, res);
        return res;
    }

    public  WrapperResponse<String> saveWeChatAccount( WechatAccountDto dto){

        AssertUtil.notBlank(dto.getWname(),"公众号名称不能为空");

        WrapperResponse<String> res = new WrapperResponse<>();
        WeChatAccountEntity entity = new WeChatAccountEntity();
        BeanUtils.copyProperties(dto,entity);

        //新增
        if(dto.getWid()==0){
//            entity.setWid(Long.valueOf(String.valueOf(idService.genId())));
            entity.setCreatetime(new Date());

            int wid = weChatAccountMapper.insert(entity);
            res.setMsg(String.valueOf(wid));

        }
        //编辑
        else{
            weChatAccountMapper.updateByPrimaryKeySelective(entity);
            res.setMsg(String.valueOf(entity.getWid()));
        }


        return  res;
    }

}
